/**
 * @program: leetcode075
 * @author: JCM
 * @create: 2023-02-20 08:26
 * @description:
 **/
public class Main {
    public static void main(String[] args) {

    }

    // 计数排序法
    public void sortColors(int[] nums) {
        int s0 = 0, s1 = 0, s2 = 0;
        for(int i = 0; i < nums.length; i++) {
            if(nums[i] == 0) s0++;
            if(nums[i] == 1) s1++;
            if(nums[i] == 2) s2++;
        }
        int count = 0;
        while(s0 != 0) {
            nums[count++] = 0;
            s0--;
        }
        while(s1 != 0) {
            nums[count++] = 1;
            s1--;
        }
        while(s2 != 0) {
            nums[count++] = 2;
            s2--;
        }
    }

    /**
     * 交换法
     * @param nums
     */
    public void sortColors2(int[] nums) {
        int zero = -1;
        int two = nums.length-1;
        int i = 0;
        while(i <= two) {
            if(nums[i] == 0) {
                zero++;
                swap(nums,zero,i);
                i++;
            }else if(nums[i] == 1) {
                i++;
            }else {
                swap(nums,i,two);
                two--;
            }
        }
    }
    private void swap(int[] nums,int a, int b) {
        int temp = nums[a];
        nums[a] = nums[b];
        nums[b] = temp;
    }
}
